package appapi.controller.oider;

import appapi.Request.Basic.EquipSelfchecjItemReq;
import appapi.Request.DelSelfItemReq;
import appapi.Vo.AddEquipSelfItemVo;
import appapi.Vo.EditEquipSelfItemBatchVo;
import appapi.Vo.EditEquipSelfItemVo;
import appapi.common.ResponseBean;
import appapi.entity.basic.EquipselfcheckitemEntity;
import appapi.service.EquipselfcheckitemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 请求对象
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/05/27 14:56
 */
@Api(tags = "设备自检项目")
@RestController
@RequestMapping("/api/equipSelfcheckItem")
public class EquipSelfcheckitemController {
    private static final Logger logger = LoggerFactory.getLogger(EquipSelfcheckitemController.class);
    @Autowired
    private EquipselfcheckitemService equipselfcheckitemService;
    /**
     * 列表
     */
    @GetMapping("/getList")
    @ApiOperation(value = "获取列表")
    public ResponseBean<EquipselfcheckitemEntity> list(@Valid EquipSelfchecjItemReq req) {
        try {
            List<EquipselfcheckitemEntity> dataList = equipselfcheckitemService.getList(req);
            return ResponseBean.success(dataList);
        } catch (Exception e) {
            logger.error("获取列表异常", e);
            return ResponseBean.fail("获取列表异常");
        }
    }

    /**
     * 信息
     */
    @GetMapping("/getDetail")
    @ApiOperation(value = "获取详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "selfItemId", dataType = "long", value = "设备自检项目Id",required = true)
    })
    public ResponseBean<EquipselfcheckitemEntity> detail(@Valid Long selfItemId) {
        try {
            if (selfItemId == null) {
                return ResponseBean.fail("ID不能为空");
            }
            EquipselfcheckitemEntity entity = equipselfcheckitemService.detail(selfItemId);
            if (entity != null) {
                return ResponseBean.success(entity);
            } else {
                return ResponseBean.fail("未查询到记录");
            }
        } catch (Exception e) {
            logger.error("获取详情异常", e);
            return ResponseBean.fail("获取详情异常");
        }
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增信息")
    @PostMapping(value = "/addInfo", produces = "application/json;charset=UTF-8")
    public ResponseBean addInfo(@Validated @RequestBody AddEquipSelfItemVo vo) {
        try {
            EquipselfcheckitemEntity entity = vo.vo2Entity();
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            int count = equipselfcheckitemService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("名称已存在，不能重复添加");
            }
            int iRet = equipselfcheckitemService.insert(entity);
            if (iRet > 0) {
                return ResponseBean.success("保存成功");
            } else {
                return ResponseBean.fail("保存失败");
            }
        } catch (Exception e) {
            logger.error("新增异常", e);
            return ResponseBean.fail("新增异常");
        }
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改信息")
    @PostMapping(value = "/editInfo", produces = "application/json;charset=UTF-8")
    public ResponseBean update(@Validated @RequestBody EditEquipSelfItemVo vo) {
        try {
            EquipselfcheckitemEntity entity = vo.vo2Entity();
            entity.setUpdateTime(LocalDateTime.now());
            int count = equipselfcheckitemService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("名称已存在，不能重复修改");
            }
            int iRet = equipselfcheckitemService.updateByPrimaryKey(entity);
            if (iRet > 0) {
                return ResponseBean.success("更新成功");
            } else {
                return ResponseBean.fail("更新失败");
            }
        } catch (Exception e) {
            logger.error("更新异常", e);
            return ResponseBean.fail("更新异常");
        }
    }

    /**
     * 批量修改
     */
    @ApiOperation(value = "批量修改信息")
    @PostMapping(value = "/editBatch", produces = "application/json;charset=UTF-8")
    public ResponseBean updateBatch(@Validated @RequestBody EditEquipSelfItemBatchVo vo) {
        try {
            int iRet = equipselfcheckitemService.updateBatch(vo);
            if (iRet > 0) {
                return ResponseBean.success("更新成功");
            } else {
                return ResponseBean.fail("更新失败");
            }
        } catch (Exception e) {
            logger.error("更新异常", e);
            return ResponseBean.fail("更新异常");
        }
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除信息")
    @PostMapping(value = "/deleteInfo", produces = "application/json;charset=UTF-8")
    public ResponseBean delete(@Validated @RequestBody DelSelfItemReq req) {
        try {
            if (req.getSelfItemId() == null) {
                return ResponseBean.fail("ID不能为空");
            }
            int iRet = equipselfcheckitemService.deleteByPrimaryKey(req.getSelfItemId());
            if (iRet == -1) {
                return ResponseBean.fail("存在引用不能删除");
            }
            else if (iRet == -2) {
                return ResponseBean.fail("对象不存在");
            }
            else if (iRet > 0) {
                return ResponseBean.success("删除成功");
            } else {
                return ResponseBean.fail("删除失败");
            }
        } catch (Exception e) {
            logger.error("删除异常", e);
            return ResponseBean.fail("删除异常");
        }
    }
}
